package ldh.im.web.service;

import ldh.common.PageResult;
import ldh.im.business.dao.DepartmentDao;
import ldh.im.business.pojo.Department;
import ldh.im.business.pojo.where.DepartmentWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: ldh
 * @date: 2021-06-06 10:17:42
*/
@Service("departmentService")
public class DepartmentService {

	@Resource
	private DepartmentDao departmentDao;
	
	@Transactional
	public Department insert(Department department) {
		int n = departmentDao.insert(department);
		if (n != 1) {
			throw new RuntimeException("Department insert error!");
		}
		return department;
	}
	
	@Transactional
	public Department insertSelective(Department department) {
		int n = departmentDao.insert(department);
		if (n != 1) {
			throw new RuntimeException("Department insertSelective error!");
		}
		return department;
	}

	@Transactional
	public Department updateById(Department department) {
		int  n = departmentDao.updateById(department);
		if (n != 1) {
			throw new RuntimeException("Department update error!");
		}
        return department;
     }

	@Transactional
	public Department updateNotNullById(Department department) {
		int  n = departmentDao.updateNotNullById(department);
		if (n != 1) {
			throw new RuntimeException("Department update error!");
		}
        return department;
     }


    @Transactional
    public void deleteById(Long id) {
		int n = departmentDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("Department delete error!");
		}
    }

	@Transactional(readOnly = true)
	public Department getById(Long id) {
		Department department = departmentDao.getById(id);
		return department;
    }
    
	
	@Transactional(readOnly = true)
	public PageResult<Department> findByDepartmentWhere(DepartmentWhere departmentWhere) {
		long total = departmentDao.findTotalByDepartmentWhere(departmentWhere);
		List<Department> imageList = new ArrayList<Department>();
		if (total > 0) {
			imageList  = departmentDao.findByDepartmentWhere(departmentWhere);
		}
		
		return new PageResult<Department>(departmentWhere, total, imageList);
	}

	@Transactional(readOnly = true)
	public List<Department> findAll(DepartmentWhere departmentWhere) {
		List<Department> departmentList = departmentDao.findByDepartmentWhere(departmentWhere);
		return departmentList;
	}
}
